package cn.com.gf.cmp.am.message;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;

public class MessageTransform {

    //委托转换 委托标识0
	public ResponseMessage responseTransform(Map<String,Object> data){
		int functionNo = 333002;
		/*Timestamp timestamp = (Timestamp)data.get("d_datetime");
		DateTime time = new DateTime(timestamp.getTime());
		String timeStr = time.toString("yyyy-MM-dd hh:mm:ss:SSSSS");*/
		String timeStr = data.get("offertime").toString();
		ResponseMessage message = new ResponseMessage(functionNo,timeStr);
		Map<String,Object> dataMap = new HashMap<String,Object>();
		
		dataMap.put("message_type", data.get("message_type").toString());
		dataMap.put("entrust_no", data.get("contractnum").toString());
		dataMap.put("init_date", timeStr.substring(0,8));
		//dataMap.put("batch_no", data.get("l_batch_serial_no").toString());
		//dataMap.put("report_no", data.get("l_report_serial_no").toString());
		//dataMap.put("seat_no", data.get("vc_report_seat"));
		dataMap.put("entrust_time", timeStr.substring(8,14));
		dataMap.put("entrust_price",data.get("orderprice").toString());
		dataMap.put("entrust_amount", data.get("orderqty").toString());
		//dataMap.put("withdraw_amount", data.get("l_cancel_amount").toString());
		dataMap.put("withdraw_amount", "");
		dataMap.put("stock_code", data.get("stkid"));
		if(data.get("ordertype").toString().contains("B")){
			dataMap.put("entrust_bs", "1");
		}else if(data.get("ordertype").toString().contains("S")){
			dataMap.put("entrust_bs", "2");
		}else{
			dataMap.put("entrust_bs", "0");
		}
        //dataMap.put("entrust_type", "0");
		//dataMap.put("entrust_status", data.get("c_entrust_status").toString());
		dataMap.put("entrust_status", "0");
		dataMap.put("fund_account", data.get("acctid").toString());
		dataMap.put("report_account", data.get("regid").toString());
//		dataMap.put("error_no", "0");
//		dataMap.put("error_info", "");
//		dataMap.put("order_id", "");
//		dataMap.put("orig_order_id", "");
		dataMap.put("branch_no", data.get("branch_no").toString());
		message.getData().add(dataMap);
		return message;
	}

	//撤单转换 撤单标识2 撤单状态F  委托数量=撤单数量
	public ResponseMessage responseCancelTransform(Map<String,Object> data){
		int functionNo = 333002;
		/*Timestamp timestamp = (Timestamp)data.get("d_datetime");
		DateTime time = new DateTime(timestamp.getTime());*/
		String timeStr = data.get("offertime").toString();
		ResponseMessage message = new ResponseMessage(functionNo,timeStr);
		Map<String,Object> dataMap = new HashMap<String,Object>();

		dataMap.put("message_type", data.get("message_type").toString());
		dataMap.put("entrust_no", data.get("contractnum").toString());
		dataMap.put("init_date", timeStr.substring(0,8));
//		dataMap.put("batch_no", data.get("l_batch_serial_no").toString());
//		dataMap.put("report_no", data.get("l_report_serial_no").toString());
//		dataMap.put("seat_no", data.get("vc_report_seat"));
		dataMap.put("entrust_time", timeStr.substring(8,14));
		dataMap.put("entrust_price",data.get("orderprice").toString());
		dataMap.put("entrust_amount", data.get("withdrawqty").toString());
		dataMap.put("withdraw_amount", data.get("withdrawqty").toString());
		dataMap.put("stock_code", data.get("stkid"));
		if(data.get("ordertype").toString().contains("B")){
			dataMap.put("entrust_bs", "1");
		}else if(data.get("ordertype").toString().contains("S")){
			dataMap.put("entrust_bs", "2");
		}else{
			dataMap.put("entrust_bs", "0");
		}
//		dataMap.put("entrust_type", "0");
		dataMap.put("entrust_status", "F");
		dataMap.put("fund_account", data.get("acctid").toString());
		dataMap.put("report_account", data.get("regid").toString());
//		dataMap.put("error_no", "0");
//		dataMap.put("error_info", "");
//		dataMap.put("order_id", "");
//		dataMap.put("orig_order_id", "");
		dataMap.put("branch_no", data.get("branch_no").toString());
		message.getData().add(dataMap);
		return message;
	}




	public RequestMessage requestTransform(Map<String,Object> data){
		int functionNo = 270020;
		/*Timestamp timestamp = (Timestamp)data.get("d_datetime");
		DateTime time = new DateTime(timestamp.getTime());
		String timeStr = time.toString("yyyy-MM-dd hh:mm:ss:SSSSS");*/
		String timeStr = data.get("knocktime").toString();
		RequestMessage message = new RequestMessage(functionNo,timeStr);
		Map<String,Object> dataMap = new HashMap<String,Object>();

		dataMap.put("message_type", data.get("message_type").toString());
		dataMap.put("entrust_no", data.get("contractnum").toString());

		dataMap.put("batch_no", data.get("contractnum").toString());
		//dataMap.put("report_no", data.get("report_no").toString());
		//dataMap.put("seat_no", data.get("vc_report_seat"));
		
		dataMap.put("message_type", data.get("message_type").toString());
		dataMap.put("function_id", "270020");
		dataMap.put("init_date", timeStr.substring(0,8));
		dataMap.put("exchange_type", data.get("exchid").toString());
		//dataMap.put("branch_no", "");
		//dataMap.put("entrust_no", data.get("l_entrust_serial_no").toString());
		//dataMap.put("report_no", data.get("l_report_serial_no").toString());
		//dataMap.put("order_id", "");
		//dataMap.put("orig_order_id", "");
		if(data.get("ordertype").toString().contains("B")){
			dataMap.put("report_bs", "1");
		}else if(data.get("ordertype").toString().contains("S")){
			dataMap.put("report_bs", "2");
		}else{
			dataMap.put("report_bs", "0");
		}
		//dataMap.put("report_seat", data.get("vc_report_seat"));
		dataMap.put("stock_code", data.get("stkid"));
		dataMap.put("report_account", data.get("regid"));
		//dataMap.put("opp_account_str", "");
		//处理dealno 中多余的 '|'
		//String dealNo = data.get("vc_deal_no").toString();
		//String trimDealNo = dealNo.indexOf('|') == -1 ? dealNo : dealNo.substring(dealNo.indexOf('|') + 1);
		dataMap.put("business_id_str", data.get("knockcode").toString());
		dataMap.put("business_no_str", data.get("knockcode").toString());
		dataMap.put("business_amount_str", data.get("knockqty").toString());
		dataMap.put("business_price_str", data.get("knockprice").toString());
		dataMap.put("business_time_str", timeStr.substring(8,14));
		dataMap.put("fund_account", data.get("acctid").toString());
//		dataMap.put("report_time", "");
//		dataMap.put("extern_code", "");
//		dataMap.put("issue_flag", "");
//		dataMap.put("action_str", "");
//		dataMap.put("busi_data", "");
//		dataMap.put("record_no", "");
//		dataMap.put("data_id", "");
		dataMap.put("branch_no", data.get("branch_no").toString());
		message.getData().add(dataMap);
		return message;
	}
}
